﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FUSE
{
    class CreationScreen : Game
    {
        KeyboardState oldKeyboardState;
        public int ScreenSelection;
        //which of the ships the player has selected
        public int ShipSelection;

        //Sprites
        Sprite background;
        
        Sprite armorShip;
        Sprite speedShip;
        Sprite normalShip;

        Sprite SelectedShipPic;

        Sprite launchButton;
        Sprite upArrow;
        Sprite downArrow;

        //textures
        Texture2D armourShipTex;
        Texture2D speedShipTex;
        Texture2D normalShipTex;



        SpriteBatch sb;
        GraphicsDeviceManager graphics;

        public CreationScreen(GraphicsDeviceManager graphics)
        {
            this.graphics = graphics;
        }

        public void LoadContent(ContentManager contentManager)
        {
            background.LoadContent(contentManager, "CreationMenuBackground");
            ScaleBackground(background);

            //load sprites
            armorShip.LoadContent(contentManager, "Armor");
            speedShip.LoadContent(contentManager, "SpeedShip");
            normalShip.LoadContent(contentManager, "NormalShipRough");
            SelectedShipPic.LoadContent(contentManager, "Armor");

            launchButton.LoadContent(contentManager, "launchButtonTex");
            upArrow.LoadContent(contentManager, "upArrow");
            downArrow.LoadContent(contentManager, "downArrow");

            //load textures
            armourShipTex = contentManager.Load<Texture2D>("Armor");
            normalShipTex = contentManager.Load<Texture2D>("NormalShipRough");
            speedShipTex = contentManager.Load<Texture2D>("SpeedShip");

        }

        public void Initialize()
        {
            sb = new SpriteBatch(graphics.GraphicsDevice);

            ScreenSelection = MyConstants.ICreationScreen;

            background = new Sprite(graphics);

            armorShip = new Sprite(graphics);
            speedShip = new Sprite(graphics);
            normalShip = new Sprite(graphics);
            SelectedShipPic = new Sprite(graphics);

            launchButton = new Sprite(graphics);
            upArrow = new Sprite(graphics);
            downArrow = new Sprite(graphics);

            background.SetX(0);
            background.SetY(0);

            //set default ship to first ship in the list
            ShipSelection = MyConstants.FirstShip;
            
            base.Initialize();
        }

        public void Update(GameTime gameTime)
        {
            HandleButtonControls();

            Draw();

            oldKeyboardState = Keyboard.GetState();

            base.Update(gameTime);
        }

        public void Draw()
        {
            //first line may be inefficient
            //ScaleBackground(background);

            SetButtonLocation();

            background.Draw(sb);

            DrawMenuButtons();
        }

        private void ScaleBackground(Sprite backgroundSprite)
        {
            Vector2 tempScale;
             
            tempScale.Y = ((float)this.graphics.PreferredBackBufferHeight / (float)backgroundSprite.Size.Height);
            tempScale.X = ((float)this.graphics.PreferredBackBufferWidth / (float)backgroundSprite.Size.Width);
           
            backgroundSprite.SetScale(tempScale);
        }

        

        public void SetButtonLocation()
        {


            //TODO calcuate button offset for each button position
            int buttonXOffset = (this.graphics.PreferredBackBufferWidth * 1 / 20);
            int buttonYOffset = (this.graphics.PreferredBackBufferHeight * 1 / 20);

            //set up arrow button location
            upArrow.SetX((int)(Math.Sqrt(
                Math.Pow(((upArrow.Size.Width / 2) - (upArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            upArrow.SetY((int)(Math.Sqrt(
                Math.Pow(((upArrow.Size.Height / 2) - (upArrow.Size.Height / 2)), 2)) + (buttonYOffset)));

            //set down arrow button location
            downArrow.SetX((int)(Math.Sqrt(
                Math.Pow(((downArrow.Size.Width / 2) - (downArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            downArrow.SetY((int)(Math.Sqrt(
                Math.Pow(((downArrow.Size.Height / 2) - (downArrow.Size.Height / 2)), 2)) + (this.graphics.PreferredBackBufferHeight - downArrow.Size.Height - buttonYOffset)));

            //set launch button location
            launchButton.SetX((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Width / 2) - (launchButton.Size.Width / 2)), 2)) + (this.graphics.PreferredBackBufferWidth - launchButton.Size.Width - buttonXOffset)));
            launchButton.SetY((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Height / 2) - (launchButton.Size.Height / 2)), 2)) + (buttonYOffset)));

            SelectedShipPic.SetX((int)(Math.Sqrt(
               Math.Pow(((downArrow.Size.Width / 2) - (downArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            SelectedShipPic.SetY((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Height / 2) - (launchButton.Size.Height / 2)), 2)) + (this.graphics.PreferredBackBufferHeight/2 - SelectedShipPic.Size.Height/2)));

        }

        private void DrawMenuButtons()
        {
            upArrow.Draw(sb);
            downArrow.Draw(sb);
            launchButton.Draw(sb);

            if (ShipSelection == MyConstants.ArmoredShip)
            {
                SelectedShipPic.SetTexture(armourShipTex);
            }
            else if (ShipSelection == MyConstants.NormalShip)
            {
                SelectedShipPic.SetTexture(normalShipTex);
            }
            else if (ShipSelection == MyConstants.SpeedShip)
            {
                SelectedShipPic.SetTexture(speedShipTex);
            }

            SelectedShipPic.Draw(sb);
        }

        //increments or decrements the menu selection based on key press
        private void HandleButtonControls()
        {                        

                if ( (Keyboard.GetState().IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyUp(Keys.Up)) )
                {
                    upArrow.SetColor(Color.Red);

                    //decrements player selection
                    if (ShipSelection < MyConstants.LastShip)
                    {
                        ShipSelection++;
                    }
                }
                else
                {
                    upArrow.SetColor(Color.White);
                }

                if ( (Keyboard.GetState().IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyUp(Keys.Down)) )
                {
                    downArrow.SetColor(Color.Red);

                    // 3 is the last item
                    if (ShipSelection > MyConstants.FirstShip)
                    {
                        ShipSelection--;
                    }
                }
                else
                {
                    downArrow.SetColor(Color.White);
                }

                if ((Keyboard.GetState().IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter)) ||
                    (Keyboard.GetState().IsKeyDown(Keys.Space) && oldKeyboardState.IsKeyUp(Keys.Space)))
                {
                    //launch
                    //will normally be set to game screen
                    ScreenSelection = MyConstants.GalaxyScreen;                  
                    launchButton.SetColor(Color.Red);
                }
                else
                {
                    launchButton.SetColor(Color.White);
                }
            
            
        }
    }
}
